<script>
import {login} from "@/api/user";

export default {
  name: "index",
  data() {
    return {
      labelPosition: 'left',
      loginForm: {
        username: '',
        password: ''
      }
    }
  },
  methods: {
    onSubmit() {
      // 发送http请求   验证用户的名字和密码是否正确  axios
      login(this.loginForm).then(response => {
        if (response.code == 200) {
          this.$message.success(response.message)
          // 跳转路由   主页面
          this.$router.push("/home")
        } else {
          this.$message.error(response.message)
        }
      })
    }
  }
}
</script>

<template>
  <div id="container">
    <h1>登录</h1>
    <el-form :label-position="labelPosition" label-width="80px" :model="loginForm">
      <el-form-item label="用户名">
        <el-input v-model="loginForm.username"></el-input>
      </el-form-item>
      <el-form-item label="密码">
        <el-input type="password" v-model="loginForm.password"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">立即登录</el-button>
      </el-form-item>
    </el-form>
    <router-link to="/register">没有账号?立即注册</router-link>
  </div>
</template>

<style scoped>
#container {
  width: 400px;
  height: 300px;
  box-shadow: 0 0 15px #ccc;
  margin: 100px auto;
  border-radius: 15px;
  padding: 20px;
  text-align: center;
}

h1 {
  color: #42b983;
  margin-bottom: 50px;
}

</style>